﻿using System;
using System.Linq;
using System.Threading;
using NPMonitor.Attributes;
using NPMonitor.DataModel;
using NPMonitor.Utility;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;

namespace Buy360Test
{
    public class BuyTest : WebJobBase
    {
        protected override void ExecuteJob(HttpWatch.Plugin plugin)
        {
            Console.WriteLine("Starting Buy360Test - ");

            try
            {
                WebDriverHelper.WebDriver = WebDriver;
                WebDriverHelper.DefaultTimeoutSecs = DefaultTimeoutSecs;
                WebDriver.Navigate().GoToUrl("http://www.360buy.com/");
                //try
                //{
                //    Control.WaitEx(plugin, -1, true, true, 0);
                //    var warnTip = WebDriver.FindElement(By.TagName("a"));
                //    warnTip.Click();
                //    Control.WaitEx(plugin, DefaultTimeoutSecs, true, true, 0);
                //}
                //catch
                //{ }

                //SenarioLogin(web);

                SenarioBookToCart(plugin);

                SenarioMoveBookFromCart(plugin);

                //SenarioLogout(web);
            }
            catch (Exception ex)
            {
                ExecutionResult.ErrorMessage = ex.Message;
                if (ex.InnerException != null)
                {
                    ExecutionResult.ErrorMessage = ex.Message + " -----> " + ex.InnerException.Message;
                }
                ExecutionResult.ResultStatus = JobResultStatus.Fail;
            }

            Console.WriteLine("Buy360Test - " + " Finished");
            return;
        }

        #region Secnarios

        [Scenario("Buy360Test_001", "Buy360Test Job Login", "Test Buy360Test Job Login", null)]
        public void SenarioLogin(HttpWatch.Plugin plugin)
        {
            Console.WriteLine("Starting Senario1");

            var loginbar = WebDriverHelper.GetById(WebDriver, "loginbar");
            var btnLogin = WebDriverHelper.GetWebElementsByLinkText(WebDriver, "[登录]")[0];
            btnLogin.Click();

            //用户名
            var txtUserName = WebDriverHelper.GetById(WebDriver, "loginname");
            txtUserName.Clear();
            txtUserName.SendKeys("dengke_009@163.com");

            //密码
            var txtPassword = WebDriverHelper.GetById(WebDriver, "loginpwd");
            txtPassword.Clear();
            txtPassword.SendKeys("dengke456987@#");

            //登陆
            var btnsubmit = WebDriverHelper.GetById(WebDriver, "loginsubmit");
            btnsubmit.Click();

            Console.WriteLine("Senario1 Finished");
        }

        [Scenario("Buy360Test_002", "Buy360Test Job 002", "Test Buy360Test Job 002", null)]
        public void SenarioBookToCart(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Buy360Test_002";
            Console.WriteLine("Starting Senario2");

            var divCategory = WebDriverHelper.GetById(WebDriver, "_JD_ALLSORT");

            var bookLink = WebDriverHelper.GetByLinkText(divCategory, "图书");
            bookLink.Click();

            String mwh = WebDriver.CurrentWindowHandle;

            var bookImg = WebDriverHelper.GetByXPath(WebDriver, "/html/body/div[5]/div/div[2]/div/div[4]/div[2]/ul/li/a/img");
            bookImg.Click();

            var s = WebDriver.WindowHandles;
            var popupHandle = s.LastOrDefault().ToString();
            WebDriver.SwitchTo().Window(popupHandle);

            var initCartUrlDiv = WebDriverHelper.GetById(WebDriver, "choose");

            var initCartUrl = WebDriverHelper.GetById(initCartUrlDiv, "InitCartUrl"); // WebDriverHelper.GetByClassName(WebDriver, "btn-append");

            initCartUrl.Click();

            Console.WriteLine("Senario2 Finished");
        }

        [Scenario("Buy360Test_003", "Buy360Test Job 003", "Test Buy360Test Job 003", null)]
        public void SenarioMoveBookFromCart(HttpWatch.Plugin plugin)
        {
            plugin.NextPageComment = "Buy360Test_003";
            Console.WriteLine("Starting Senario3");
            //WebDriverWait wait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(100));

            var gotoShoppingCart = WebDriverHelper.GetById(WebDriver, "GotoShoppingCart");
            //var gotoShoppingCart = wait.Until(
            //   d =>
            //   {
            //       return WebDriverHelper.GetById(WebDriver, "GotoShoppingCart");
            //   }
            //  );
            gotoShoppingCart.Click();

            var removeBatch = WebDriverHelper.GetById(WebDriver, "remove-batch");
            //var removeBatch = wait.Until(
            //   d =>
            //   {
            //       return WebDriverHelper.GetById(WebDriver, "remove-batch");
            //   }
            //  );
            removeBatch.Click();

            var btnRemoveConfirm = WebDriverHelper.GetById(WebDriver, "btnRemoveConfirm");
            btnRemoveConfirm.Click();

            Console.WriteLine("Senario3 Finished");
        }

        [Scenario("Buy360Test_004", "Buy360Test Job 004", "Test Buy360Test Job 004", null)]
        public void SenarioLogout(HttpWatch.Plugin plugin)
        {
            Console.WriteLine("Executing Senario4");

            var btnLogout = WebDriverHelper.GetByClassName(WebDriver, "link-logout");
            btnLogout.Click();
        }

        #endregion
    }
}
